package dao;

import pojo.Note;
import util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ImplementNote implements NoteDao{
    /**
     * 添加笔记，先搜寻id增加到哪--id最好用int类型，这里麻烦了
     * @param note
     * @return
     */
    @Override
    public boolean addNote(Note note) {
        int id=0;
        boolean flag=false;
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //获取所有笔记
        ArrayList<Integer> listid=new ArrayList<Integer>();
        ResultSet rs=jdbc.selectsql("select * from biji");
        while(true){
            try {
                if (!rs.next()) break;
                listid.add(Integer.valueOf(rs.getString("id")));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        id= listid.get(0);
        //遍历寻找id的最大值
for(int i=0;i< listid.size();i++){
    if(listid.get(i)>id){
        id=listid.get(i);
    }
}
        id++;
        String newid= String.valueOf(id);
        String sql="insert into biji values('"+newid+"','"+note.getContent()+"','"+note.getTitle()+"','"+note.getType()+"','"+note.getUsename()+"','"+note.getStyle()+"')";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 修改笔记内容
     * @param id    获取客户端请求修改的笔记的id，根据id修改笔记内容
     * @param note 封装好了的笔记对象
     * @return
     */
    @Override
    public boolean updateNote(String id,Note note) {
        boolean flag=false;
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql="update biji set title='"+note.getTitle()+"',content='"+note.getContent()+"',type='"+note.getType()+"',style='"+note.getStyle()+"' where id='"+id+"'";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 删除笔记
     * @param usename
     * @param title
     * @return
     */
    @Override
    public boolean deleteNote(String usename,String title) {
        boolean flag=false;
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql="delete from biji where usename='"+usename+"' and title='"+title+"'";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 根据id删除note
     * @param id
     * @return
     */
    public boolean delNote(String id){
    boolean flag=false;
    JDBC jdbc=new JDBC();
    try {
        jdbc.dbconn();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    String sql="delete from biji where id='"+id+"'";
    int i=jdbc.addsql(sql);
    if(i>0)flag=true;
    else flag=false;
    jdbc.closeconn();
    return flag;
    }


    @Override
    public Note note(String id, String title, String content, String type, String usename) {
        Note note=new Note();
        note.setId(id);
        note.setTitle(title);
        note.setContent(content);
        note.setType(type);
        note.setUsename(usename);
        return note;
    }

    /**
     * 查看所有用户发布的所有笔记---for系统管理员
     * @return
     */
    @Override
    public ArrayList<Note> showAllNotes() {
        ArrayList<Note> listnotesOfUser = new ArrayList<>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from biji");
        while(true){
            try {
                if (!rs.next()) break;
                Note note=new Note();
                note.setStyle(Integer.parseInt(rs.getString("style")));
                note.setId(rs.getString("id"));
                note.setTitle(rs.getString("title"));
                note.setContent(rs.getString("content"));
                note.setType(rs.getString("type"));
                note.setUsename(rs.getString("usename"));
                listnotesOfUser.add(note);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        jdbc.closeconn();
        return listnotesOfUser;
    }

    /**
     * 查看某个具体用户的所有笔记
     * @param usename
     * @return
     */
    public ArrayList<Note> userallnote(String usename){
        ArrayList<Note> useallnote = new ArrayList<>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from biji where usename='"+usename+"'");
        while(true){
            try {
                if (!rs.next()) break;
                Note note=new Note();
                note.setStyle(Integer.parseInt(rs.getString("style")));
                note.setId((rs.getString("id")));
                note.setTitle(rs.getString("title"));
                note.setContent(rs.getString("content"));
                note.setType(rs.getString("type"));
                note.setUsename(rs.getString("usename"));
                useallnote.add(note);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        jdbc.closeconn();
        return useallnote;
    }

    /**
     * 根据id查找某个笔记--用于用户修改笔记
     * @param id
     * @return
     */
    public Note findNote(String id){
        ImplementNote implementNote=new ImplementNote();
        ArrayList<Note> listnotes=new ArrayList<>();
        listnotes=implementNote.showAllNotes();
        Note note=new Note();
        for (Note listnote : listnotes) {
            if(listnote.getId().equals(id)){
                note.setStyle(listnote.getStyle());
                note.setId(listnote.getId());
                note.setUsename(listnote.getUsename());
                note.setType(listnote.getType());
                note.setContent(listnote.getContent());
                note.setTitle(listnote.getTitle());
                break;
            }
        }
        return note;
    }

    public ArrayList<Note> user_checkednote(String usename){
        ArrayList<Note> useallnotes=new ArrayList<>();
        ArrayList<Note> checkednotes=new ArrayList<>();
        ImplementNote implementNote=new ImplementNote();
        useallnotes=implementNote.userallnote(usename);
        for (Note useallnote : useallnotes) {
            if(useallnote.getStyle()==0){
                Note checkednote=new Note();
                checkednote.setStyle(0);
                checkednote.setUsename(usename);
                checkednote.setTitle(useallnote.getTitle());
                checkednote.setContent(useallnote.getContent());
                checkednote.setId(useallnote.getId());
                checkednote.setType(useallnote.getType());
                checkednotes.add(checkednote);
            }
        }
return checkednotes;
    }

    public boolean updatestyle(String id){
        boolean flag=false;
        int newstyle=1;
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql="update biji set style='"+newstyle+"' where id='"+id+"'";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }
}
